package com.upcode.weighing.common.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * Java用的是Unicode 编码char 型变量的范围是0-65535 无符号的值，可以表示 65536个字符，基本上地球上的字符可被全部包括了
     *
     * 汉字基本集中在[19968,40869]之间,共有20901个汉字
     * unicode编码范围：
     * 汉字：[0x4e00,0x9fa5]（或十进制[19968,40869]）
     * 数字：[0x30,0x39]（或十进制[48, 57]）
     * 小写字母：[0x61,0x7a]（或十进制[97, 122]）
     * 大写字母：[0x41,0x5a]（或十进制[65, 90]）
     */
    private static Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");

    public static int length(String str){
        if(str == null || str.length() == 0){
            return 0;
        }

        int count = 0;
        StringBuffer buffer = new StringBuffer(str);
        for(int i = 0,len = buffer.length(); i < len;i++){
            Matcher macher = pattern.matcher(String.valueOf(buffer.charAt(i)));
            if(macher.find()){
                count += 2;
            }else{
                count++;
            }
        }
       return count;
    }
    public static String subString(String str,int maxLength){
        if(str == null || str.trim().isEmpty()){
            return str;
        }
        if(str.length() * 2 <= maxLength){
            return str;
        }


        StringBuffer buffer = new StringBuffer(str);
        int counter = 0;
        int i = 0,len = buffer.length();
        for(; i < len;i++){
            Matcher macher = pattern.matcher(String.valueOf(buffer.charAt(i)));
            if(macher.find()){
                counter += 2;
            }else{
                counter++;
            }
            if(counter > maxLength){
                break;
            }
        }
        /*System.out.println("str = "+str);
        System.out.println("str length  = "+len);
        System.out.println("counter = " +counter);
        System.out.println("i = "+i);*/

        if(i == len){
            return str;
        }else{
            return str.substring(0,i);
        }
    }

    /*public static void main(String[] args){
        String str  = "这是A哪个s是!注销++大.V公司大幅";
        int length = StringUtils.length(str);
        String s = StringUtils.subString(str,20);
        System.out.println("=========="+length);
        System.out.println("=========="+s);
    }*/


    public static String time2ToEagleTimeStr(LocalDateTime dateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        return formatter.format(dateTime);
    }

    public static String nowTimeStr(String format){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        String strDate2 = dtf.format(LocalDateTime.now());
        return strDate2;
    }

    public static boolean isEmpty(String str){
        return str!=null && str.trim().length()>0;
    }

    /**
     * 字符串的脱敏处理
     * 信息字符串中间的1/3长度（有余数+1），转换为*    来进行处理，如果脱敏后明文前后不一样长，则明文多的那一部分在后面。
     * 例如：字符串长度为10，则对中间的4位进行脱敏处理。1809224895  —》180****895
     * 字符串长度为7，则对中间的3位进行脱敏处理。陕A12345—》陕A***45
     * 字符串长度为11，则对中间4位进行脱敏处理，  18092248956  -》180****8956
     * @param sensitiveStr
     * @return
     */
    public static String insensitive(String sensitiveStr){
        int insenLen=(sensitiveStr.length()+2)/3;
        int insenStartIndex=(sensitiveStr.length()-insenLen)/2;
        StringBuffer buffer = new StringBuffer(sensitiveStr);
        for(int i=0;i<insenLen;i++){
            buffer=buffer.replace(insenStartIndex+i, (insenStartIndex+i)+1, "*");
        }
        String insensitiveStr=buffer.toString();
        return insensitiveStr;
    }

    public static String dateToStr(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }

    public static String dateToStr2(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMM");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }
    public static String dateToStr3(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }
    public static LocalDate strToDate(String dateStr) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate beginDate = LocalDate.parse(dateStr, fmt);
        return beginDate;
    }
    public static String dateToStr(LocalDate localDate,String format) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
        String dateStr = localDate.format(fmt);
        return dateStr;
    }
    public static long daysOfTwoDate(LocalDate afterDate,LocalDate befortDate) {
        long days=afterDate.toEpochDay()-befortDate.toEpochDay();
        return days;
    }

    public static long daysToToday(LocalDate befortDate) {
        long days=daysOfTwoDate(LocalDate.now(),befortDate);
        return days;
    }
//    public static void main(String[] args) {
//        String str=insensitive("1809224895");
//        String str2=insensitive("陕A12345");
//        String str3=insensitive("18092248956");
//
//        System.out.println(str);
////        System.out.println(str2);
////        System.out.println(str3);
//
//    }
}
